<template>
  <div class="app-container">
    <Title
      :show-btn="true"
      :is-show-back="false"
      name="码类型管理"
      type="primary"
      btn-name="添加码类型"
      icon-class="el-icon-plus"
      @btnClick="handleAdd"
    />
    <el-input
      placeholder="搜索码名称"
      style="width: 220px; margin: 16px 0px"
      size="small"
      clearable
      v-model="queryParams.cardName"
      @keyup.enter.native="handleQuery"
    >
      <i slot="prefix" class="el-input__icon el-icon-search"></i>
    </el-input>
    <el-table v-loading="loading" :data="categoryList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="排序号"
        prop="orderNum"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="码名称" prop="cardName" />
      <el-table-column label="码编码" prop="cardCode" />

      <el-table-column label="码属单位" prop="department" />
      <el-table-column label="状态" width="90">
        <template slot-scope="scope">
          <div>{{ scope.row.cardStatus == 1 ? "启用" : "禁用" }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="添加时间" prop="createTime" /> -->
      <el-table-column label="添加时间">
        <template slot-scope="scope">
          <div>{{ timeCutOut(scope.row.createTime) }}</div>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
            >修改</el-button
          >
          <i
            style="
              display: inline-block;
              height: 14px;
              border-left: 2px solid #eaedf1;
              margin: auto 11px;
            "
          />
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.cardStatus == 1"
            @click="disableEnabled(scope.row, 0)"
            >禁用</el-button
          >

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.cardStatus == 0"
            @click="disableEnabled(scope.row, 1)"
            >启用</el-button
          >
          <i
            style="
              display: inline-block;
              height: 14px;
              border-left: 2px solid #eaedf1;
              margin: auto 11px;
            "
          />
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改码类型对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="400px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="码名称" prop="cardName">
          <el-input
            v-model="form.cardName"
            placeholder="请输入码名称"
            maxlength="50"
            :validate-event="false"
          />
        </el-form-item>
        <el-form-item label="码编码" prop="cardCode">
          <el-input
            v-model="form.cardCode"
            placeholder="请输入码编码"
            maxlength="50"
            :validate-event="false"
          />
        </el-form-item>
        <el-form-item label="码属单位" prop="department">
          <el-input
            v-model="form.department"
            placeholder="请输入码属单位"
            maxlength="100"
            :validate-event="false"
          />
        </el-form-item>
        <el-form-item label="排序号" prop="orderNum">
          <el-input
            v-model="form.orderNum"
            placeholder="请输入排序号"
            maxlength="30"
            :validate-event="false"
          />
        </el-form-item>
        <el-form-item class="labelText" label="是否启用" prop="cardStatus">
          <el-radio-group v-model="form.cardStatus">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="isHint"
      :show-close="true"
      width="354px"
      top="0"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <div class="deletHint">{{ hintText }}</div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isHint = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="updateCard"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Title from "@/components/ShareTitle";
import {
  queryCardList,
  addCard,
  updateCard,
  deleteCard,
} from "@/api/codeManagement/codeType";
export default {
  name: "codeType",
  components: { Title },
  data() {
    return {
      categoryList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardName: undefined,
      },
      form: {
        cardStatus: 1,
      },
      hintText: "",
      isHint: false,
      title: "",
      open: false,
      total: 0,
      // 表单校验
      rules: {
        cardName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "排序号不能为空", trigger: "blur" },
        ],
        cardCode: [
          { required: true, message: "卡编码不能为空", trigger: "blur" },
        ],
        department: [
          { required: true, message: "码属单位不能为空", trigger: "blur" },
        ],
      },
      statusOptions: [
        {
          value: 1,
          label: "启用",
        },
        {
          value: 0,
          label: "禁用",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询分类列表
    getList() {
      this.loading = true;
      queryCardList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.categoryList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 时间处理
    timeCutOut(time) {
      var str = time;
      var after = str.split("T")[0];
      var later = str.split("T")[1];
      return after + " " + later;
    },
    // 禁用启用
    disableEnabled(row, value) {
      this.form = JSON.parse(JSON.stringify(row));
      this.form.cardStatus = value;
      delete this.form.createTime;
      delete this.form.updateTime;
      if (value === 1) {
        this.hintText = `确定要启用 ${row.cardName} 吗 ？？？`;
      } else {
        this.hintText = `确定要禁用 ${row.cardName} 吗 ？？？`;
      }
      this.isHint = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.orderNum === "0") {
            this.$message.error("排序值最小值为1");
            return false;
          }
          if (this.form.id != undefined) {
            this.updateCard();
          } else {
            addCard(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    updateCard() {
      updateCard(this.form).then((response) => {
        this.msgSuccess("修改成功");
        this.open = false;
        this.getList();
        this.isHint = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除卡类型"' + row.cardName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return deleteCard({ id: ids });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加码类型";
    },
    // 表单重置
    reset() {
      this.form = {
        cardName: undefined,
        orderNum: undefined,
        department: undefined,
        cardCode: undefined,
        cardStatus: 1,
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = "修改码类型";
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  padding-top: 0px;
}
.deletHint{
  text-align: center;
}
</style>